(function($, undefined) {
	$.widget('ui.aywPhoto', {
		options: {
		},
		photoData: null,
		_create: function() {
			var self = this, _jsonData = $.parseJSON($('figure', this.element).attr('data'));
			self.photoData = _jsonData;
			
			$('.edit', self.element).on('click', function(){
				self.editPhoto();
				return false;
			});
			$('.delete', self.element).on('click', function() {
				self.delPhoto();
				return false;
			});
			
			$('.cover', self.element).on('click', function() {
				self.setCover();
				return false;
			});
			
			$('.btn_b').live('click', function(){
				self.doUpdatePhoto();
				return false;
			});
		},//endcreate
		//删除单张图片
		delPhoto: function (album_id, photo_id){
			var self = this;
			if(confirm('你确定要删除这张图片么？')){
				$.post(U('photo/Manage/delete_photo'),{id:self.photoData.photoId,albumId:self.photoData.albumId},function(data){
					if(data==1){
						$(self).closest('figure').remove();
						ui.success('删除成功！');
						return;
					}else{
						ui.error('删除失败！');
					}
				});
			}
		},
		
		//将我的一张图片设置为该专辑}的封面
		setCover: function (){
			var self = this;
			if(confirm('你要将这张图片设置为封面么？')){
				$.post(U('photo/Manage/set_cover'),{photoId:self.photoData.photoId,albumId:self.photoData.albumId},function(data){
					if(data==1){
						ui.success('封面设置成功！');
					}else if(data==-1){
						ui.error('该图片不存在！');
					}else{
						ui.error('当前封面已是该图片，或设置失败！');
					}
				});
			}
		},
		
		//编辑图片
		editPhoto: function (){
			var self = this;
			//ui.box.load(U('photo/Manage/edit_photo_tab')+'&aid='+album_id+'&pid='+photo_id,{title:'编辑图片'});
			var html = $('#photo_edit_box')
						.find('#albumId').val(self.photoData.albumId).end()
						.find('#photoId').val(self.photoData.photoId).end()
						.find('#name').val(self.photoData.name).end()
						.find('#desc').val(self.photoData.desc).end()
						.html();
			console.log(self.photoData);
			ui.box.show(html, {title:'编辑图片',closeable:true});
		},
		
		doUpdatePhoto: function (){
			var name	=	$('#name').val();
			var desc	=	$('#desc').val();
			var self	= 	this;
			if(!name)	{ 
				ui.error('图片名字不能为空！');
				return false;
			}
			$.post(U('photo/Manage/do_update_photo'),{id:self.photoData.photoId,name:name,albumId:self.photoData.albumId, desc:desc},function(data){
			    if(data==1){
					/*if(albumId!=albumIdold){
						if(nextid==id||nextid==''){
							location.href=U('photo/Index/album')+'&id='+album_id+'&uid='+_UID_;
						}else{
							location.href=U('photo/Index/photo')+'&id='+nextid+'&aid='+album_id+'&uid='+_UID_;
						}
						return;
					}else{
						$('.photoName').html(name);
					}*/
					//location.reload();
					$('.title', self.element).text(name);
					$('.desc', self.element).text(desc);
					ui.box.close();
					ui.success('修改成功！');
				}else{
					ui.box.close();
					ui.error('图片信息无变化！');
				}
			});
		}					
	})
})(jQuery);

